#!/usr/bin/env python3
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

from cmk.agent_based.v2 import Result, Service, State
from cmk.plugins.oracle.agent_based.oracle_locks import (
    check_oracle_locks,
    inventory_oracle_locks,
    parse_oracle_locks,
)

INFO = [
    [
        "TUX12C",
        "",
        "2985",
        "ora12c.local",
        "sqlplus@ora12c.local (TNS V1-V3)",
        "46148",
        "oracle",
        "633",
        "NULL",
        "NULL",
    ],
    [
        "newdb",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "3782",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
    [
        "newdb1",
        "25",
        "15231",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13275",
        "oracle",
        "SYS",
        "1800",
        "VALID",
        "1",
        "407",
        "1463",
        "ol6131",
        "sqlplus@ol6131 (TNS V1-V3)",
        "13018",
        "oracle",
        "SYS",
    ],
]


def test_discovery() -> None:
    assert list(inventory_oracle_locks(parse_oracle_locks(INFO))) == [
        Service(item="TUX12C"),
        Service(item="newdb"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
        Service(item="newdb1"),
    ]


def test_check_no_locks() -> None:
    assert list(
        check_oracle_locks("TUX12C", {"levels": (1800, 3600)}, parse_oracle_locks(INFO))
    ) == [
        Result(
            state=State.OK,
            summary="No locks existing",
        ),
    ]


def test_check_locks() -> None:
    assert list(
        check_oracle_locks("newdb", {"levels": (1800, 3600)}, parse_oracle_locks(INFO))
    ) == [
        Result(
            state=State.CRIT,
            summary="locktime 1 hour 3 minutes (!!) Session (sid,serial, proc) 25,15231,13275 machine ol6131 osuser oracle object: . ; ",
        )
    ]


def test_check_too_many_locks() -> None:
    assert list(
        check_oracle_locks("newdb1", {"levels": (1800, 3600)}, parse_oracle_locks(INFO))
    ) == [
        Result(
            state=State.WARN,
            summary="more then 10 locks existing!",
        )
    ]
